﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace DimensionalMeasurement
{
    public partial class FormImageProcesses : Form
    {
        HWindow hwindow;
        HObject image;
        HTuple width, height;
        string Camera;

        public FormImageProcesses(HObject image, string Camera)
        {
            InitializeComponent();
            hwindow = new HWindow();
            this.image = image;
            this.Camera = Camera;
            bJEVLateralEdge.Visible = false;
            bJELeftVerticalEdge.Visible = false;
            bJERightVerticalEdge.Visible = false;
            bJEBox.Visible = false;
            bJPDLLateralEdge.Visible = false;
            bJEVLateralEdge1.Visible = false;
            if (Regex.IsMatch(Camera, "正") && Regex.IsMatch(Camera, PublicSet.Myset.PJELocation))
            {
                bJPDLLateralEdge.Text = "极片陶瓷边";
                bJEVLateralEdge.Visible = true;
                bJELeftVerticalEdge.Visible = true;
                bJERightVerticalEdge.Visible = true;
                bJPDLLateralEdge.Visible = true;
                if (PublicSet.Myset.JETwoCamera && PublicSet.Myset.PJELocation.Contains("左")) bJERightVerticalEdge.Visible = false;
                if (PublicSet.Myset.JETwoCamera && PublicSet.Myset.PJELocation.Contains("右")) bJELeftVerticalEdge.Visible = false;
            }
            if (Regex.IsMatch(Camera, "负") && Regex.IsMatch(Camera, PublicSet.Myset.NJELocation))
            {
                bJEVLateralEdge.Visible = true;
                bJELeftVerticalEdge.Visible = true;
                bJERightVerticalEdge.Visible = true;
                bJPDLLateralEdge.Visible = true;
                if (PublicSet.Myset.JETwoCamera && PublicSet.Myset.NJELocation.Contains("左")) bJERightVerticalEdge.Visible = false;
                if (PublicSet.Myset.JETwoCamera && PublicSet.Myset.NJELocation.Contains("右")) bJELeftVerticalEdge.Visible = false;
            }

            if (Regex.IsMatch(Camera, "正") && Regex.IsMatch(Camera, PublicSet.Myset.PJELocation1) && PublicSet.Myset.JETwoCamera)
            {
                if (PublicSet.Myset.PJELocation1.Contains("左")) bJELeftVerticalEdge.Visible = true;
                if (PublicSet.Myset.PJELocation1.Contains("右")) bJERightVerticalEdge.Visible = true;
                bJEVLateralEdge1.Visible = true;
            }
            if (Regex.IsMatch(Camera, "负") && Regex.IsMatch(Camera, PublicSet.Myset.NJELocation1) && PublicSet.Myset.JETwoCamera)
            {
                if (PublicSet.Myset.NJELocation1.Contains("左")) bJELeftVerticalEdge.Visible = true;
                if (PublicSet.Myset.NJELocation1.Contains("右")) bJERightVerticalEdge.Visible = true;
                bJEVLateralEdge1.Visible = true;
            }
            if ((Camera.Contains("负左上") && PublicSet.Myset.NullSliceN) || (Camera.Contains("正左上") && PublicSet.Myset.NullSliceP))
            {
                bNullSice.Visible = true;
            }
            else
            {
                bNullSice.Visible = false;
            }
            if (PublicSet.Myset.DetectMode == "2相机正负双测")
            {
                bJPLateralEdge.Text = "极片左横边";
                bJPVerticalEdge.Text = "极片左竖边";
                bJPVBevelEdge.Text = "V角左斜边";
                bJPVLateralEdge.Text = "V角左横边";

                bJPRightLateralEdge.Visible = true;
                bJPRightVerticalEdge.Visible = true;
                bJPRightVBevelEdge.Visible = true;
                bJPRightVLateralEdge.Visible = true;
                if ((Camera.Contains("负左上") && PublicSet.Myset.NullSliceN) || (Camera.Contains("正右上") && PublicSet.Myset.NullSliceP))
                {
                    bNullSice.Visible = true;
                }
                else
                {
                    bNullSice.Visible = false;
                }

            }

        }
        ToolTip tp1 = new ToolTip();
        private void FormImage_Load(object sender, EventArgs e)
        {

            #region

            BeginInvoke(new Action(() =>
            {
                ToolTip tp1 = new ToolTip();
                tp1.Show("单击按钮开始画框，右击鼠标结束画框。右击按钮更改参数", pictureBox, 150, 300, 5000);
            }));
            #endregion

            if (image != null)
            {
                hwindow.OpenWindow(0, 0, pictureBox.Width, pictureBox.Height, pictureBox.Handle, "visible", "");
                HOperatorSet.GetImageSize(image, out width, out height);
                hwindow.SetPart(0, 0, height.I, width.I);
                hwindow.SetDraw("margin");
                hwindow.SetLineWidth(2);
                hwindow.SetFont("-Courier New-18-*-*-*-*-1-");
                hwindow.DispObj(image);
                pictureBox.MouseWheel += HMouseWheel;
            }

        }
        private void HMouseWheel(object sender, MouseEventArgs e)
        {
            if (image != null)
            {
                Tool.HMouseWheel(e, hwindow, image);
            }
        }
        private void Measureline(MeasureLine line, bool b)
        {
            hwindow.DispObj(image);
            hwindow.SetColor("red");
            propertyGrid.SelectedObject = line;
            double row = 200, col = 400, phi = 0, length1 = 500, length2 = 80;
            if (line.行坐标 <= height && line.列坐标 <= width || line.行坐标 < 0 || line.列坐标 < 0)
            {
                row = line.行坐标; col = line.列坐标; length1 = line.半宽; length2 = line.半高; phi = line.弧度;
            }
            hwindow.DrawRectangle2Mod(row, col, phi, length1, length2, out row, out col, out phi, out length1, out length2);
            line.行坐标 = Math.Round(row, 3);
            line.列坐标 = Math.Round(col, 3);
            line.弧度 = Math.Round(phi, 3);
            line.半宽 = Math.Round(length1, 3);
            line.半高 = Math.Round(length2, 3);
            if (b)
            {

                ImageProcesses.MeasureLine(image, hwindow, line, out HTuple rowBegin, out HTuple colBegin, out HTuple rowEnd, out HTuple colEnd);
                line.起始行坐标 = Math.Round(rowBegin.D, 3);
                line.起始列坐标 = Math.Round(colBegin.D, 3);
                line.末尾行坐标 = Math.Round(rowEnd.D, 3);
                line.末尾列坐标 = Math.Round(colEnd.D, 3);
                hwindow.SetColor("blue");
                ImageProcesses.DispRectangle2(hwindow, line);

            }
            else
            {
                hwindow.SetColor("green");
                hwindow.DispRectangle2(row, col, phi, length1, length2);
            }
            propertyGrid.SelectedObject = line;
        }

        private void pTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Tool.FormMove(Handle);
        }

        /// <summary>
        /// 启用控件
        /// </summary>
        void show()
        {
            bJPLateralEdge.Enabled = true;
            bJPVerticalEdge.Enabled = true;
            bJPVBevelEdge.Enabled = true;
            bJPVLateralEdge.Enabled = true;
            bJEVLateralEdge.Enabled = true;
            bJELeftVerticalEdge.Enabled = true;
            bJERightVerticalEdge.Enabled = true;
            //  bJEBox.Enabled = true;
            bJPDLLateralEdge.Enabled = true;

            bJPRightLateralEdge.Enabled = true;
            bJPRightVerticalEdge.Enabled = true;
            bJPRightVBevelEdge.Enabled = true;
            bJPRightVLateralEdge.Enabled = true;

            bMarkLateralEdge.Enabled = true;
            bMarkVerticalEdge.Enabled = true;
            bNullSice.Enabled = true;
            bClose.Enabled = true;
        }
        /// <summary>
        /// 禁用控件
        /// </summary>
        void hide()
        {
            bJPLateralEdge.Enabled = false;
            bJPVerticalEdge.Enabled = false;
            bJPVBevelEdge.Enabled = false;
            bJPVLateralEdge.Enabled = false;
            bJEVLateralEdge.Enabled = false;
            bJELeftVerticalEdge.Enabled = false;
            bJERightVerticalEdge.Enabled = false;
            // bJEBox.Enabled = false;
            bJPDLLateralEdge.Enabled = false;

            bJPRightLateralEdge.Enabled = false;
            bJPRightVerticalEdge.Enabled = false;
            bJPRightVBevelEdge.Enabled = false;
            bJPRightVLateralEdge.Enabled = false;

            bMarkLateralEdge.Enabled = false;
            bMarkVerticalEdge.Enabled = false;
            bNullSice.Enabled = false;
            bClose.Enabled = false;
        }


        private void bJPLateralEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;

            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[0]; break;
                case "负右上": line = PublicSet.measureLines[4]; break;
                case "负左下": line = PublicSet.measureLines[8]; break;
                case "负右下": line = PublicSet.measureLines[12]; break;

                case "正左上": line = PublicSet.measureLines[16]; break;
                case "正右上": line = PublicSet.measureLines[20]; break;
                case "正左下": line = PublicSet.measureLines[24]; break;
                case "正右下": line = PublicSet.measureLines[28]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line;
                propertyGrid.Refresh();
                return;
            }
            hide();
            this.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }


        private void bJPVerticalEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[1]; break;
                case "负右上": line = PublicSet.measureLines[5]; break;
                case "负左下": line = PublicSet.measureLines[9]; break;
                case "负右下": line = PublicSet.measureLines[13]; break;

                case "正左上": line = PublicSet.measureLines[17]; break;
                case "正右上": line = PublicSet.measureLines[21]; break;
                case "正左下": line = PublicSet.measureLines[25]; break;
                case "正右下": line = PublicSet.measureLines[29]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line;
                propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJPVBevelEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[2]; break;
                case "负右上": line = PublicSet.measureLines[6]; break;
                case "负左下": line = PublicSet.measureLines[10]; break;
                case "负右下": line = PublicSet.measureLines[14]; break;

                case "正左上": line = PublicSet.measureLines[18]; break;
                case "正右上": line = PublicSet.measureLines[22]; break;
                case "正左下": line = PublicSet.measureLines[26]; break;
                case "正右下": line = PublicSet.measureLines[30]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line;
                propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }
        private void bJPVLateralEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[3]; break;
                case "负右上": line = PublicSet.measureLines[7]; break;
                case "负左下": line = PublicSet.measureLines[11]; break;
                case "负右下": line = PublicSet.measureLines[15]; break;

                case "正左上": line = PublicSet.measureLines[19]; break;
                case "正右上": line = PublicSet.measureLines[23]; break;
                case "正左下": line = PublicSet.measureLines[27]; break;
                case "正右下": line = PublicSet.measureLines[31]; break;
                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJEVLateralEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();

            if (Regex.IsMatch(Camera, "负"))
            {
                line = PublicSet.measureLines[34];
            }
            else
            {
                line = PublicSet.measureLines[38];
            }

            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJELeftVerticalEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            if (Regex.IsMatch(Camera, "负"))
            {
                line = PublicSet.measureLines[35];
            }
            else
            {
                line = PublicSet.measureLines[39];
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJERightVerticalEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            if (Regex.IsMatch(Camera, "负"))
            {
                line = PublicSet.measureLines[36];
            }
            else
            {
                line = PublicSet.measureLines[40];
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJEBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null)
            {
                return;
            }
            MeasureLine line = new MeasureLine();
            if (Regex.IsMatch(Camera, "负"))
            {
                line = PublicSet.measureLines[37];
            }
            else
            {
                line = PublicSet.measureLines[41];
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, false);
            show();
            propertyGrid.Refresh();
        }
        private void bJPDLLateralEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            if (Regex.IsMatch(Camera, "负"))
            {
                line = PublicSet.measureLines[32];
            }
            else
            {
                line = PublicSet.measureLines[33];
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }


        private void bJPRightLateralEdge_MouseDown_1(object sender, MouseEventArgs e)
        {
            if (image == null) return;

            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[4]; break;
                case "负左下": line = PublicSet.measureLines[12]; break;
                case "正右上": line = PublicSet.measureLines[16]; break;
                case "正右下": line = PublicSet.measureLines[24]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();

        }

        private void bJPRightVerticalEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[5]; break;
                case "负左下": line = PublicSet.measureLines[13]; break;

                case "正右上": line = PublicSet.measureLines[17]; break;
                case "正右下": line = PublicSet.measureLines[25]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJPRightVBevelEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[6]; break;
                case "负左下": line = PublicSet.measureLines[14]; break;

                case "正右上": line = PublicSet.measureLines[18]; break;
                case "正右下": line = PublicSet.measureLines[26]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bJPRightVLateralEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[7]; break;
                case "负左下": line = PublicSet.measureLines[15]; break;

                case "正右上": line = PublicSet.measureLines[19]; break;
                case "正右下": line = PublicSet.measureLines[27]; break;
                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bMarkLateralEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[42]; break;
                case "负右上": line = PublicSet.measureLines[44]; break;
                case "负左下": line = PublicSet.measureLines[46]; break;
                case "负右下": line = PublicSet.measureLines[48]; break;

                case "正左上": line = PublicSet.measureLines[50]; break;
                case "正右上": line = PublicSet.measureLines[52]; break;
                case "正左下": line = PublicSet.measureLines[54]; break;
                case "正右下": line = PublicSet.measureLines[56]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bMarkVerticalEdge_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();
            switch (Camera)
            {
                case "负左上": line = PublicSet.measureLines[43]; break;
                case "负右上": line = PublicSet.measureLines[45]; break;
                case "负左下": line = PublicSet.measureLines[47]; break;
                case "负右下": line = PublicSet.measureLines[49]; break;

                case "正左上": line = PublicSet.measureLines[51]; break;
                case "正右上": line = PublicSet.measureLines[53]; break;
                case "正左下": line = PublicSet.measureLines[55]; break;
                case "正右下": line = PublicSet.measureLines[57]; break;

                default: break;
            }
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }

            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bNullSice_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();

            if (Camera.Contains("负")) line = PublicSet.measureLines[58];
            if (Camera.Contains("正")) line = PublicSet.measureLines[59];
            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }

            hide();
            pictureBox.Focus();
            Measureline(line, false);
            show();
            propertyGrid.Refresh();
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 2)
            {
                if (image != null)
                {
                    HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                    HOperatorSet.SetPart(hwindow, 0, 0, height.I, width.I);
                    HOperatorSet.DispObj(image, hwindow);
                }
            }
        }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (image != null)
            {
                try
                {
                    HTuple Row, Col, gray1;
                    HOperatorSet.GetMposition(hwindow, out Row, out Col, out _);
                    HOperatorSet.GetGrayval(image, Row, Col, out gray1);
                    label1.Text = $"阈值:{gray1} ";
                }
                catch (Exception)
                {
                }
            }
        }


        private void bJEVLateralEdge1_MouseDown(object sender, MouseEventArgs e)
        {
            if (image == null) return;
            MeasureLine line = new MeasureLine();

            if (Regex.IsMatch(Camera, "负"))
            {
                line = PublicSet.measureLines[60];
            }
            else
            {
                line = PublicSet.measureLines[61];
            }

            if (e.Button == MouseButtons.Right)
            {
                propertyGrid.SelectedObject = line; propertyGrid.Refresh();
                return;
            }
            hide();
            pictureBox.Focus();
            Measureline(line, true);
            show();
            propertyGrid.Refresh();
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            PublicSet.SavaMeasureLine();
            tp1.Dispose();
            Close();
        }
    }
}
